package com.fan.shop.businesslogic;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.log4j.Logger;

import com.fan.shop.domain.StockReqLine;
import com.fan.shop.domain.StockReqLineId;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.StockReqLineService;
import com.fan.shop.services.StoreService;
import com.fan.shop.services.UserService;

public class StockReqLineCreateProcess extends ProcExec<Integer> {

	private static Logger logger = Logger
			.getLogger(StockReqLineCreateProcess.class);
	
	private String requisitionNo;
	private String officerCode;
	private String storeCode;
	private String itemCode;
	private BigDecimal price;
	private int qty;
	private Date transactionDate;
	private String lineStatus;

	private StockReqLineService stockReqLineService;
	private StoreService storeService;
	private UserService userService;

	public String getRequisitionNo() {
		return requisitionNo;
	}

	public void setRequisitionNo(String requisitionNo) {
		this.requisitionNo = requisitionNo;
	}


	public String getOfficerCode() {
		return officerCode;
	}

	public void setOfficerCode(String officerCode) {
		this.officerCode = officerCode;
	}

	public String getStoreCode() {
		return storeCode;
	}

	public void setStoreCode(String storeCode) {
		this.storeCode = storeCode;
	}

	public String getItemCode() {
		return itemCode;
	}

	public void setItemCode(String itemCode) {
		this.itemCode = itemCode;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public int getQty() {
		return qty;
	}

	public void setQty(int qty) {
		this.qty = qty;
	}

	public Date getTransactionDate() {
		return transactionDate;
	}

	public void setTransactionDate(Date transactionDate) {
		this.transactionDate = transactionDate;
	}

	public String getLineStatus() {
		return lineStatus;
	}

	public void setLineStatus(String lineStatus) {
		this.lineStatus = lineStatus;
	}

	public StockReqLineService getStockReqLineService() {
		return stockReqLineService;
	}

	public void setStockReqLineService(StockReqLineService stockReqLineService) {
		this.stockReqLineService = stockReqLineService;
	}

	public StoreService getStoreService() {
		return storeService;
	}

	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		StockReqLineCreateProcess.logger = logger;
	}

	@Override
	public Integer processImpl() {

		if (stockReqLineService.hasStockReqLine(requisitionNo, itemCode)) {
			setErrorMessage("Rquisition: " + requisitionNo + "#" + itemCode
					+ " already exists!");
			return ShopGlobalVariables.FAILED;
		}
		if (!storeService.hasStore(storeCode)) {
			setErrorMessage("Store: " + storeCode + " doesn't exists!");
			return ShopGlobalVariables.FAILED;
		}
		if (!userService.hasUser(officerCode)) {
			setErrorMessage("User: " + officerCode + " doesn't exists!");
			return ShopGlobalVariables.FAILED;
		}

		StockReqLine stockReqLine = new StockReqLine();
		StockReqLineId stockReqLineId = new StockReqLineId();
		stockReqLineId.setRequisitionNo(requisitionNo);
		stockReqLineId.setItemCode(itemCode);
		stockReqLine.setId(stockReqLineId);
		stockReqLine.setLineStatus(lineStatus);
		stockReqLine.setOfficerCode(officerCode);
		stockReqLine.setPrice(price);
		stockReqLine.setQty(qty);
		stockReqLine.setStoreCode(storeCode);
		stockReqLine.setTransactionDate(transactionDate);

		stockReqLineService.addStockReqLine(stockReqLine);

		return ShopGlobalVariables.SUCCESS;
	}

}
